// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "title": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 30
      }
    ],
    "title": "店铺名称",
    "label": "店铺名称"
  },
  "categories": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "店铺分类",
    "label": "店铺分类"
  },
  "delivery_timeliness": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "minimum": 10,
        "maximum": 300
      }
    ],
    "title": "配送时效",
    "label": "配送时效"
  },
  "score": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0,
        "maximum": 5
      }
    ],
    "title": "店铺评分",
    "label": "店铺评分"
  },
  "monthly_sales": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "月销售量",
    "defaultValue": 0,
    "label": "月销售量"
  },
  "total_sales": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "总销售量",
    "defaultValue": 0,
    "label": "总销售量"
  },
  "region": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "店铺地址",
    "label": "店铺地址"
  },
  "address": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 50
      }
    ],
    "title": "详细地址",
    "label": "详细地址"
  },
  "location": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "object"
      }
    ],
    "title": "详细地址",
    "label": "详细地址"
  },
  "area": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "object"
      }
    ],
    "title": "所属区域",
    "label": "所属区域"
  },
  "area_all": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "array"
      }
    ],
    "title": "所属区域",
    "label": "所属区域"
  },
  "start_business_hours1": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "开始营业时间段1",
    "label": "开始营业时间段1"
  },
  "end_business_hours1": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "结束营业时间段1",
    "label": "结束营业时间段1"
  },
  "start_business_hours2": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "开始营业时间段2",
    "label": "开始营业时间段2"
  },
  "end_business_hours2": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "结束营业时间段2",
    "label": "结束营业时间段2"
  },
  "logo": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "file"
      }
    ],
    "title": "店铺logo",
    "label": "店铺logo"
  },
  "photo": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      },
      {
        "maxLength": 50
      }
    ],
    "title": "店铺照片",
    "label": "店铺照片"
  },
  "tel": {
    "rules": [
      {
        "format": "string"
      },
      {
        "pattern": "^\\+?[0-9-]{3,20}$"
      }
    ],
    "title": "手机号码",
    "label": "手机号码"
  },
  "start_delivery_price": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "起送费",
    "label": "起送费"
  },
  
  "is_book": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否可预订"
  },
  "is_take_away": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否可外卖"
  },
  "intro": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "minLength": 5,
        "maxLength": 500
      }
    ],
    "title": "简介",
    "label": "简介"
  },
  "create_uid": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "创建人",
    "label": "创建人"
  },
  "page_view": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0,
        "maximum": 100000000000
      }
    ],
    "title": "浏览量",
    "label": "浏览量"
  },
  "evaluate": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0,
        "maximum": 10000000
      }
    ],
    "title": "评价量",
    "label": "评价量"
  },
  "disabled_status": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "店铺状态",
    "defaultValue": true,
    "label": "店铺状态"
  },
  "business_status": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "经营状态",
    "defaultValue": true,
    "label": "经营状态"
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
